<template>
	<view>
		<fa-navbar title="入会申请"></fa-navbar>
		<view class="prompt">
			* 请完善以下信息
		</view>

		<view class="list">
			<view class="item" v-for="(v,i) in list" :key="i" :class="{flex: i != 10}">
				<view class="left">
					{{v.title}}
				</view>
				<view class="right" @click="openSelect(i)">
					<!-- status == 1 -->
					<input v-if="v.status == 1" type="text" :placeholder="v.placeholder" v-model="v.value"
						@input="inputChange(i)" placeholder-style="color:#b8b8b8;" />
					<!-- status == 2 -->
					<u-radio-group v-model="v.value" @change="radioGroupChange" v-if="v.status == 2">
						<u-radio v-for="(item, index) in radioList" :key="index" :name="item.name"
							:disabled="item.disabled" active-color='#c72a29'>
							{{item.name}}
						</u-radio>
					</u-radio-group>
					<!-- status == 3 -->
					<view class="openSelect" style="color: #b8b8b8;" v-if="v.status == 3 && v.value == ''">
						请选择<u-icon name="arrow-down"></u-icon>
					</view>
					<view class="openSelect" v-if="v.status == 3 && v.value != ''">
						{{v.value}}
					</view>
					<!-- status == 4 -->
					<view class="code" v-if="v.status == 4">
						<input type="text" :placeholder="v.placeholder" placeholder-style="color:#b8b8b8;"
							v-model="v.value" @input="inputChange(i)" />
						<view class="btn" @click="getVerificationCode">
							{{ buttonText }}
						</view>
					</view>
					<!-- status == 5 -->
					<view class="textarea" v-if="v.status == 5" style="margin-top: -45rpx;">
						<textarea :placeholder="v.placeholder" placeholder-style="color:#b8b8b8;" v-model="v.value"
							@input="inputChange(i)" />
					</view>

				</view>
			</view>
			<view class="upload">
				<view class="title">
					上传企业营业执照
				</view>
				<view class="type">
					(支持jpg,png格式，大小不超过2m)
				</view>
				<view class="image">
					<u-upload :action="vuex_config.upload.uploadurl" :file-list="fileList" :header="header"
						:form-data="formdata" @on-uploaded="successUpload" @on-error="errorUpload" @on-remove="remove"
						:max-count="1" width="150" height="150" :show-progress='false'></u-upload>
				</view>
			</view>
		</view>
		<view class="nextPage" @click="next">
			下一页
		</view>
		<view class="" style="height: 30rpx;">

		</view>
		<!-- 籍贯 -->
		<provincesAndCities @hometownSelected="handleHometownSelection" v-if="nativePlace" />
		<!-- 出生年月 -->
		<u-picker mode="time" v-model="dateShow" @confirm='confirm'></u-picker>
		<!-- 政治面貌 -->
		<u-select v-model="politicalShow" :list="political" @confirm='politicalConfirm'></u-select>
		<!-- 学历 -->
		<u-select v-model="educationalBackgroundShow" :list="educationalBackground"
			@confirm='educationalBackgroundConfirm'></u-select>
	</view>



</template>

<script>
	import provincesAndCities from '@/components/provincesAndCities/provincesAndCities.vue'; // 省市picker组件
	export default {
		components: {
			provincesAndCities
		},
		data() {
			return {
				buttonText: '点击获取',
				countdown: 60,
				disabled: false,
				fileList: [],
				fileUrl: '',
				politicalShow: false,
				dateShow: false,
				nativePlace: false,
				educationalBackgroundShow: false,
				header: {},
				formdata: {},
				political: [],
				educationalBackground: [{
						label: '中专',
						value: 1
					},
					{
						label: '高中',
						value: 2
					},
					{
						label: '大专',
						value: 3
					},
					{
						label: '本科',
						value: 4
					},
					{
						label: '研究生',
						value: 5
					},
					{
						label: '博士',
						value: 6
					},
					{
						label: '其他',
						value: 7
					}
				],
				radioList: [{
						name: '男',
						disabled: false
					},
					{
						name: '女',
						disabled: false
					},

				],
				// u-radio-group的v-model绑定的值如果设置为某个radio的name，就会被默认选中
				list: [{
						title: '姓名',
						value: '',
						status: 1,
						placeholder: '请输入姓名',
						check: '请输入姓名'
					},
					{
						title: '性别',
						value: '',
						status: 2,
						placeholder: '请选择性别',
						check: '请选择性别'
					},
					{
						title: '民族',
						value: '',
						status: 1,
						placeholder: '例如：汉族',
						check: '请输入民族',
					},
					{
						title: '籍贯',
						value: '',
						status: 3,
						placeholder: '请选择籍贯',
						check: '请选择籍贯',
					},
					{
						title: '出生年月',
						value: '',
						status: 3,
						placeholder: '请选择出生年月',
						check: '请选择出生年月',
					},
					{
						title: '政治面貌',
						value: '',
						status: 3,
						placeholder: '请选择政治面貌',
						check: '请选择政治面貌'
					},
					{
						title: '学历',
						value: '',
						status: 3,
						placeholder: '请选择学历',
						check: '请选择学历'
					},
					{
						title: '身份证号码',
						value: '',
						status: 1,
						placeholder: '请填写身份证号码',
						check: '请输入身份证号码'
					},
					{
						title: '手机号码',
						value: '',
						status: 1,
						placeholder: '11位手机号',
						check: '请输入手机号码'
					},
					{
						title: '手机短信验证码',
						value: '',
						status: 4,
						placeholder: '请输入验证码',
						check: '请输入验证码'
					},
					{
						title: '社会职务',
						value: '',
						status: 5,
						placeholder: '社会职务是指在社会组织、社团、公益组织等社会机构中担任的职务。如没有，则填“无”',
						check: '请输入社会职务'
					},
					{
						title: '所属企业及职务',
						value: '',
						status: 1,
						placeholder: '例如: XX科技有限公司董事长',
						check: '请输入所属企业及职务'
					},
				]
			}
		},
		created() {
			this.header = {
				token: this.vuex_token || '',
				uid: this.vuex_user.id || 0
			};
			this.getSelectList()
			const form1 = uni.getStorageSync('firstForm')
			if (form1) {
				this.list = form1
			}
			const image = uni.getStorageSync('image')
			if (image) {
				const fileList = [{
					url: this.$u.http.config.baseUrl + image
				}]; // 将字符串数据转换成 fileList 数组格式
				this.fileList = fileList
				this.fileUrl = image
			}
		},
		methods: {
			async getVerificationCode() {
				if (this.disabled) return; // 如果按钮已禁用，直接返回

				// 发送请求获取验证码，这里只是一个示例
				if (this.list[8].value) {
					// 你需要替换成实际的发送请求的代码
					let data = {
						mobile: this.list[8].value,
						event: 'mini'
					}
					// 这里暂时使用 setTimeout 模拟发送请求的过程
					const res = await this.$api.getCode(data)
					console.log(res, 'res');
					if (res.code == 1) {
						// 设置按钮禁用状态，并开始倒计时
						this.disabled = true;
						this.buttonText = `${this.countdown} 秒后重发`;

						// 开始倒计时
						let timer = setInterval(() => {
							this.countdown--;
							if (this.countdown <= 0) {
								// 倒计时结束，恢复按钮状态
								clearInterval(timer);
								this.disabled = false;
								this.buttonText = '点击获取';
								this.countdown = 60;
							} else {
								this.buttonText = `${this.countdown} 秒后重发`;
							}
						}, 1000);
					} else {
						uni.showModal({
							title: '友情提示',
							content: res.msg,
							showCancel: false
						})
					}


				} else {
					uni.showModal({
						title: '友情提示',
						content: '请先输入手机号码',
						showCancel: false
					})
				}

			},
			educationalBackgroundConfirm(e) {
				this.list[6].value = e[0].label
			},
			politicalConfirm(e) {
				this.list[5].value = e[0].label
			},
			async getSelectList() {
				let res = await this.$api.getSelectList()
				if (!res.code) {
					this.$u.toast(res.msg)
					return
				}

				const modifiedData = res.data.political.map(item => {
					return {
						value: item.value,
						label: item.name
					};
				});
				this.political = modifiedData
				console.log(this.political, 'this.political');
			},
			inputChange(i) {
				// console.log("输入的值为：", this.list);

			},
			confirm(e) {
				console.log(e, 'ee');
				this.list[4].value = e.year + '-' + e.month + '-' + e.day
			},
			openSelect(index) {
				switch (index) {
					case 3:
						this.nativePlace = !this.nativePlace;
						break;
					case 4:
						this.dateShow = true;
						break;
					case 5:
						this.politicalShow = true;
						break;
					case 6:
						this.educationalBackgroundShow = true;
						break;
					default:
						// 默认情况下的处理逻辑
						break;
				}

			},
			async next() {
				// 判断列表中每个元素的value属性是否有值
				const hasValue = this.list.every(item => item.value);

				if (!hasValue) {
					// 如果有任何一个元素的value属性没有值，提取其对应的placeholder属性
					const missingPlaceholders = this.list.filter(item => !item.value).map(item => item.check);
					// 输出没有值的placeholder

					uni.showModal({
						title: '友情提示',
						content: missingPlaceholders[0],
						showCancel: false
					})
					// 这里可以将错误信息展示给用户或者采取其他逻辑处理
					return;
				}
				if (!this.fileUrl) {
					uni.showModal({
						title: '友情提示',
						content: '请上传营业执照',
						showCancel: false
					})
					return false
				}
				const checkMobile = /^1[3456789]\d{9}$/; // 手机号正则表达式
				if (!checkMobile.test(this.list[8].value)) {
					uni.showModal({
						title: '友情提示',
						content: '手机号码格式错误',
						showCancel: false
					})
					return false
				}

				const chekIdCard = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/; // 身份证号正则表达式
				if (!chekIdCard.test(this.list[7].value)) {
					uni.showModal({
						title: '友情提示',
						content: '身份证号码格式错误',
						showCancel: false
					})
					return false
				}
				let data = {
					mobile: this.list[8].value,
					event: 'mini',
					code: this.list[9].value
				}
				const res = await this.$api.validateCode(data)
				if (!res.code) {
					uni.showModal({
						title: '友情提示',
						content: res.msg,
						showCancel: false,
					})
					return;
				}
				uni.setStorageSync('firstForm', this.list)
				uni.setStorageSync('image', this.fileUrl)
				this.$u.route('/pages/applicationForm/applicationForm2');
			},
			successUpload(e) {
				this.changes(e)
			},
			remove(index, lists, name) {
				this.changes(lists)
			},
			changes(e) {
				console.log(e)
				let urls = [];
				e.map(item => {
					//编辑时，已存在的，response不存在
					if (!item.response) {
						urls.push(item.url)
					} else if (item.response.code) {
						urls.push(item.response.data.url);
					}
				});
				let value = urls.join(',');
				this.fileUrl = value
			},
			errorUpload(e) {
				this.$u.toast('上传失败了！');
			},
			handleHometownSelection(selectedData) {
				this.list[3].value = selectedData
			},

			radioGroupChange(e) {
				this.list[1].value = e
			},

		}
	};
</script>

<style scoped lang="scss">
	/* 可以添加页面样式 */
	.prompt {
		width: 749rpx;
		height: 57rpx;
		background-color: #f5dfe1;
		color: #d9001b;
		line-height: 57rpx;
		padding-left: 38rpx;
		font-size: 26rpx;
	}

	.list {
		padding: 0rpx 35rpx;
		font-size: 31rpx;

		.flex {
			display: flex;
		}

		.item {


			border-bottom: 3rpx solid #ececec;

			.left {
				padding: 35rpx 0rpx;
				width: 250rpx;
			}

			.right {
				padding: 35rpx 0rpx;
				color: #000;
				flex: 1;

				input {
					width: 100%;
				}

				.openSelect {
					/deep/ .u-icon__icon {
						margin-left: 20rpx;
					}
				}

				.code {
					display: flex;

					input {
						width: 268rpx;
					}

					.btn {
						padding: 0rpx 10rpx;
						height: 50rpx;
						border-radius: 7rpx;
						border: solid 1rpx #d9001b;
						text-align: center;
						line-height: 43rpx;
						color: #d9001b;
						font-size: 24rpx;
						margin-top: -7rpx;
					}
				}

				.textarea {
					width: 683rpx !important;
					height: 212rpx !important;
					background-color: #f8f7f7;
					border-radius: 10rpx;
					padding: 25rpx 20rpx;

					textarea {
						width: 630rpx !important;
						height: 170rpx !important;
					}
				}
			}

		}

		.upload {
			padding: 35rpx 0rpx;

			.title {
				font-size: 31rpx;
			}

			.type {
				font-size: 20rpx;
				color: #585858;
				margin-top: 10rpx;
			}

			.image {
				width: 156rpx;
				height: 156rpx;
				border-radius: 4rpx;
				margin-top: 20rpx;

				image {
					width: 100%;
					height: 100%;
					border-radius: 4rpx;
				}
			}

			/deep/ .u-delete-icon {
				width: 30rpx;
				height: 30rpx;
				top: 0rpx !important;
				right: 0rpx !important;
			}
		}
	}

	.nextPage {
		width: 650rpx;
		height: 67rpx;
		background-color: #d9001b;
		border-radius: 10rpx;
		color: #fff;
		text-align: center;
		line-height: 67rpx;
		font-size: 26rpx;
		margin: 30rpx auto;
	}
</style>